﻿using System;
using System.Collections.Generic;
using System.Text;
using CodeBuilder.Service;
using System.Data;

namespace CodeBuilder
{
    class VoCodesFactroy
    {
        private string cn;
        private DataSet ds;
        public VoCodesFactroy(DataSet dataSet)
        {
            ds = dataSet;
            cn = ds.Tables[0].Rows[0][2].ToString().Substring(0, 1).ToUpper();
            cn += ds.Tables[0].Rows[0][2].ToString().Substring(1).ToLower() + "Vo";
        }
        string part1 = "using System;\nusing System.Collections.Generic;\nusing System.Text;\n\n";
        private static string ns = "vo";
        /// <summary>
        /// 获取或设置将要在代码中生成的命名空间的值
        /// </summary>
        public static string nameSpace
        {
            get
            {
                return ns;
            }
            set
            {
                ns = value;
            }
        }
        //StringBuilder stringBuilder = new StringBuilder(); 用？不用？why？
        string part2;
        private void writePart2()
        {
            part2 = "namespace " + ns + "\n{\n\tpublic class " + cn + "\n\t{\n\t\t";
            string temp1 = "";
            string temp2 = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                switch (dr[1].ToString().ToLower())
                {
                    case "char":
                    case "nchar":
                    case "varchar":
                    case "nvarchar":
                        temp1 += "private string _" + dr[0].ToString() + ";";
                        temp2 += "\n\t\tpublic string " + dr[0].ToString().Substring(0, 1).ToUpper() + dr[0].ToString().Substring(1) +
                            "\n\t\t{\n\t\t\t" + "get" + "\n\t\t\t{\n\t\t\t\treturn this._" + dr[0].ToString() + ";" + "\n\t\t\t}\n\t\t\t" +
                            "set" + "\n\t\t\t{\n\t\t\t\tthis._" + dr[0].ToString() + " = value;" + "\n\t\t\t}\n\t\t}";
                        break;
                    case "int":
                        temp1 += "private int _" + dr[0].ToString() + ";";
                        temp2 += "\n\t\tpublic int " + dr[0].ToString().Substring(0, 1).ToUpper() + dr[0].ToString().Substring(1) +
                            "\n\t\t{\n\t\t\t" + "get" + "\n\t\t\t{\n\t\t\t\treturn this._" + dr[0].ToString() + ";" + "\n\t\t\t}\n\t\t\t" +
                            "set" + "\n\t\t\t{\n\t\t\t\tthis._" + dr[0].ToString() + " = value;" + "\n\t\t\t}\n\t\t}";
                        break;
                    case "datetime":
                        temp1 += "private DateTime _" + dr[0].ToString() + ";";
                        temp2 += "\n\t\tpublic DateTime " + dr[0].ToString().Substring(0, 1).ToUpper() + dr[0].ToString().Substring(1) +
                            "\n\t\t{\n\t\t\t" + "get" + "\n\t\t\t{\n\t\t\t\treturn this._" + dr[0].ToString() + ";" + "\n\t\t\t}\n\t\t\t" +
                            "set" + "\n\t\t\t{\n\t\t\t\tthis._" + dr[0].ToString() + " = value;" + "\n\t\t\t}\n\t\t}";
                        break;
                    case "decimal":
                    case "numeric":
                        temp1 += "private decimal _" + dr[0].ToString() + ";";
                        temp2 += "\n\t\tpublic decimal " + dr[0].ToString().Substring(0, 1).ToUpper() + dr[0].ToString().Substring(1) +
                            "\n\t\t{\n\t\t\t" + "get" + "\n\t\t\t{\n\t\t\t\treturn this._" + dr[0].ToString() + ";" + "\n\t\t\t}\n\t\t\t" +
                            "set" + "\n\t\t\t{\n\t\t\t\tthis._" + dr[0].ToString() + " = value;" + "\n\t\t\t}\n\t\t}";
                        break;
                    default:
                        temp1 += "private string _" + dr[0].ToString() + ";";
                        temp2 += "\n\t\tpublic string " + dr[0].ToString().Substring(0, 1).ToUpper() + dr[0].ToString().Substring(1) +
                            "\n\t\t{\n\t\t\t" + "get" + "\n\t\t\t{\n\t\t\t\treturn this._" + dr[0].ToString() + ";" + "\n\t\t\t}\n\t\t\t" +
                            "set" + "\n\t\t\t{\n\t\t\t\tthis._" + dr[0].ToString() + " = value;" + "\n\t\t\t}\n\t\t}";
                        break;
                }
                temp1 += "\n\t\t";
                temp2 += "\n\t\t";
            }
            part2 += temp1 + temp2 + "\n\t}\n}";
        }
        /// <summary>
        /// 生成 .cs 文件（推荐在线程中运行）
        /// </summary>
        public string RunInThread()
        {
            this.writePart2();
            return part1 + part2;
        }
    }
}